package com.xingkeduo.business.transformation.json;

import com.fasterxml.jackson.databind.JsonNode;
import com.xingkeduo.pojo.enums.LoginEnums;
import com.xingkeduo.pojo.query.CraftsmanLoginHistoryQO;
import com.xingkeduo.util.pojo.base.BaseJson;
import com.xingkeduo.util.pojo.enums.YesNoEnum;
import com.xingkeduo.util.pojo.exception.DataException;
import com.xingkeduo.util.pojo.exception.InputParamException;
import com.xingkeduo.util.tool.CommonUtil;
import com.xingkeduo.util.tool.RegularUtil;

/**
 * @Description:
 * @Author:gaozifeng
 * @Date 2019/3/18 13:52
 **/
public class CraftsmanLoginHistoryJTO extends BaseJson {

    /**
     * 验证登录类型    1微信授权 2手机号+验证码 3手机号+密码     login为统一入参(phone/openid)
     *
     * @param json:
     * @return : com.xingkeduo.pojo.query.CraftsmanQO
     * @author : gaozifeng
     * @date : 2019年3月18日14:13:35
     */
    public static CraftsmanLoginHistoryQO createQueryForLogin(String json) throws Throwable {
        CraftsmanLoginHistoryQO query = new CraftsmanLoginHistoryQO();
        JsonNode root = validJson(json);
        if (root == null) {
            return query;
        }
        String type = validLoginType(root, YesNoEnum.YES);
        query.setLogin(validString(root,"login",40,YesNoEnum.YES));
        query.setType(validType(root,YesNoEnum.YES));
        if("3".equals(type)){
            query.setPassword(validString(root,"password",50,YesNoEnum.YES));
            if (!RegularUtil.isSelfPhone(query.getLogin())) {
                throw new DataException(DataException.Type.DEFAULT,"手机号格式不正确");
            }
        }
        if("2".equals(type)){
            query.setMsgCode(validString(root,"msgCode",6,YesNoEnum.YES));
            if (!RegularUtil.isSelfPhone(query.getLogin())) {
                throw new DataException(DataException.Type.DEFAULT,"手机号格式不正确");
            }
        }
        return query;
    }

    /**查询手机号修改
     * @param json:
     * @return : com.xingkeduo.pojo.query.CraftsmanLoginHistoryQO
     * @author : gaozifeng
     * @date : 2019/3/18 21:48
     */
    public static CraftsmanLoginHistoryQO createQueryForUpdatePwd(String json) throws Throwable {
        CraftsmanLoginHistoryQO query = new CraftsmanLoginHistoryQO();
        JsonNode root = validJson(json);
        if (root == null)
            throw new InputParamException(InputParamException.Type.INVALID, "参数");
        query.setLogin(validString(root,"login",32,YesNoEnum.YES));
        if (!RegularUtil.isSelfPhone(query.getLogin())) {
            throw new DataException(DataException.Type.DEFAULT,"手机号格式不正确");
        }
        return query;
    }


    /**
     * 登录类型 1微信授权 2手机号+验证码 3手机号+密码
     *
     * @param node:
     * @param isRequired:
     * @return : com.xingkeduo.pojo.table.CraftsmanTO.Status
     * @author : gaozifeng
     * @date : 2019年3月18日14:00:35
     */
    public static String validLoginType(JsonNode node, YesNoEnum isRequired) throws Throwable {
        String key = "type";
        if (YesNoEnum.YES == isRequired && !node.has(key))
            throw new InputParamException(InputParamException.Type.NULL, key);

        if (node.has(key)) {
            String value = node.path(key).asText().trim();
            if (CommonUtil.isEmpty(value) || value.length() > 2)
                throw new InputParamException(InputParamException.Type.INVALID, key + " error");
            return value;
        }
        return null;
    }

    public static LoginEnums validType(JsonNode node, YesNoEnum isRequired) throws Throwable {
        String key = "type";
        if (YesNoEnum.YES == isRequired && !node.has(key))
            throw new InputParamException(InputParamException.Type.NULL, key);

        if (node.has(key)) {
            String value = node.path(key).asText().trim();
            if (!LoginEnums.validValue(value))
                throw new InputParamException(InputParamException.Type.INVALID, key + " error");
            return LoginEnums.getType(value);
        }
        return null;
    }
}
